import tushare as ts
from pymongo import MongoClient, DESCENDING, ASCENDING
import json
import re
import datetime
import time
import pandas
import os
import re
import platform
print('python v', platform.python_version())

# 连接数据库
client = MongoClient('mongodb://localhost:27017')
database = client['stocks']

# names = database.collection_names()
# for name in names:
#     if re.match(r'^stock_', name):
#         database.drop_collection(name)

# 1. 获取所有股票列表
# stock_list_df = ts.get_stock_basics()

# 如果集合的最新日期不等于最近的一个交易日，就以最新日期为start，以当前日期为end获取当前股票历史数据，并且更新数据库，插入新记录

# dd/mm/yyyy格式
# print (time.strftime("%Y-%m-%d"))
# datetime.datetime.now()
# count = 0
# D:/Projects/Test/stocks/trading-data@full.20170717/stock_data/
stock_data_path = input('Please input stock data directory path: ')

imported = []
no_data = []
# 遍历股票列表并且存入数据库
for stock_info in database['stock_list'].find({}).__iter__():   # 获取每行的index、row
    item = stock_info
    # if count < 10:
    #     count += 1
    # 2. 更新股票列表，stock_list集合使用股票代码作为索引
    stock_collection = database['stock_' + item.get('code')]
    stock_collection.create_index('date', unique=True)

    if stock_collection.find({}).count() == 0:
        file_name = stock_data_path + str(item['exchange']).lower() + item['code'] + '.csv'
        if os.path.exists(file_name):
            days_df = pandas.read_csv(file_name)
            # print(item['code'], days_df)
            daily_list = []
            for day_info in days_df.iterrows():
                # print(day_info[1])
                day_info_dict = day_info[1]  # json.loads(day_info.to_json())
                daily_list.append({
                    'date': datetime.datetime.strptime(str(day_info_dict['date']) + ' 15:00:00', '%Y-%m-%d %H:%M:%S'),
                    'open': day_info_dict['open'],
                    'close': day_info_dict['close'],
                    'high': day_info_dict['high'],
                    'change': day_info_dict['change'],
                    'low': day_info_dict['low'],
                    'volume': day_info_dict['volume'],
                    'money': day_info_dict['money'],
                    'turnover': day_info_dict['turnover']
                })
            stock_collection.insert_many(daily_list)
            print(item["code"], '导入 ', days_df['date'][len(days_df)-1], '-', days_df['date'][0], '共', len(days_df), '条数据')
        else:
            no_data.append(item["code"])
    else:
        imported.append(item["code"])
print(len(imported), '历史数据已经导入')
print(imported)
print(len(no_data), '历史数据不存在')
print(no_data)
